package com.admin.dao;

import com.admin.entity.Admin;
import com.admin.entity.Page;
import com.admin.utils.JDBCUtil;
import com.uni.entity.User;
import com.uni.service.Service;
import com.uni.utils.DruidUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Uni
 * @create 2021/12/2 19:12
 */
public class AdminDaoImpl implements AdminDao {



    @Override
    public Admin selectById(int id) {
        Connection connection = DruidUtil.getConnection();
        String sql = "select * from admin where uid = ?";
        Admin admin = null;
        try{
            PreparedStatement p = connection.prepareStatement(sql);
            p.setInt(1, id);
            ResultSet r = p.executeQuery();
            while(r.next())
                admin = new Admin(r.getInt(1), Service.user.queryUserById(r.getInt(2)));
            DruidUtil.release(connection, p, r);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return admin;
    }

    @Override
    public boolean updateUser(User user) {
        Connection connection = DruidUtil.getConnection();
        String sql = "update user set password = ? where id = ?";
        int ok = 0;
        try{
            PreparedStatement p = connection.prepareStatement(sql);
            p.setString(1, user.getPassword());
            p.setInt(2, user.getId());
            ok = p.executeUpdate();
            DruidUtil.release(connection, p, null);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return ok > 0;
    }
}
